package com.uplooking.pms.system.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.uplooking.pms.base.controller.BaseController;
import com.uplooking.pms.common.util.CommonUtil;
import com.uplooking.pms.common.vo.DwzPageBean;
import com.uplooking.pms.system.entity.Role;
import com.uplooking.pms.system.entity.User;
import com.uplooking.pms.system.service.IRoleService;
import com.uplooking.pms.system.service.IUserService;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author chair
 * @since 2017-12-01
 */
@Controller
@RequestMapping("system/role")
public class RoleController extends BaseController {

	@Autowired
	private IRoleService ir;
	
	@RequestMapping("/list")
	public String toRole(String name,DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {
		
		dwzPageBean.getCountResultMap().put("name", name);

		Wrapper wrapper = Condition.create().like("name", name);
		Page resultPage = ir.selectPage(dwzPageBean.toPage(),wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));

		return "system/role/list";
	}
	
	@RequestMapping("/insert")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", ir.selectById(id));
		return "system/user/insert";
	}
	
/*
	@RequestMapping("/list")
	public String list(String name, String phone, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {

		dwzPageBean.getCountResultMap().put("name", name);
		dwzPageBean.getCountResultMap().put("phone", phone);

		Wrapper wrapper = Condition.create().like("name", name).like("phone", phone);

		Page resultPage = ir.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));

		return "system/role/list";
	}
	

	@RequestMapping("/toInsert")
	public String toInsert(HttpServletRequest request, HttpServletResponse response) {

		return "system/user/insert";
	}

	@RequestMapping("/insert")
	@ResponseBody
	public Object insert(User user, HttpServletRequest request, HttpServletResponse response) {
		Wrapper<User> wrapper = Condition.create().eq("name", user.getName()).or().eq("phone", user.getPhone());
		User user2 = userService.selectOne(wrapper);
		if (!CommonUtil.isBlank(user2)) {
			return ajaxError("姓名或手机重复");
		}

		if (userService.insert(user)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", userService.selectById(id));
		return "system/user/insert";
	}

	@RequestMapping("/update")
	@ResponseBody
	public Object update(User user, HttpServletRequest request, HttpServletResponse response) {
		if (userService.updateAllColumnById(user)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object update(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (userService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}*/

}
